package com.changwen.currentactivity.accessibility;

import android.app.Notification;
import android.os.Parcelable;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;

import java.util.List;

/**
 * @author lcw
 * @date 2019/4/8
 * @desc
 */
public class AcbMsgUtils {

    private static final String TAG = AcbMsgUtils.class.getSimpleName();

    /**
     * 获取通知栏消息文本
     *
     * @param event
     * @return
     */
    public static String getNotifyMessage(AccessibilityEvent event) {
        Parcelable data = event.getParcelableData();
        if (data != null && data instanceof Notification) {
            Notification notification = (Notification) data;
            if (notification.tickerText == null) {
                Log.e(TAG, "++++++++ Notify Message is null");
                return "";
            }
            String notifyMessage = notification.tickerText.toString();
            Log.i(TAG, "Notify Message : " + notifyMessage);

            return notifyMessage;
        }
        return "";
    }

    public static boolean contains(AccessibilityEvent event, CharSequence target) {
        List<CharSequence> texts = event.getText();
        for (CharSequence text : texts) {
            return text.toString().contains(target);
        }
        return false;
    }

}
